/**
 * @Description:  index api module
 * @author longfa liang
 * @date 2021/7/15
 */
var redis = require('../db/redis')
var pu = require('../tool/public')
var config = require('../config/config')
var sms = require('../tool/SMS')
var svgCaptcha = require('svg-captcha')
var modeulse = require('../tool/module')
var msg = require('../tool/message')


class indexapi {
    get_config (req, res, next) {
        var con = {
            serverlist: config.startserverurl
            , qiniu: config.qiniu
            , map: config.map
        }
        pu.success('OK', con, res)
        return
    }

    get_module (req, res, next) {
        pu.success('OK', modeulse, res)
        return
    }

    async find_notice (req, res, next) {
        let userid = req.query.id
        if (userid) {
            let message = await redis.hgetall('USER_NOTICE_' + userid)
            pu.success('OK', message, res)
        } else {
            pu.success('OK', [], res)
        }
    }


    async send_notice (req, res, next) {
        var userlist = req.body.userlist //用户ID列表
        var template_name = req.body.template_name//表单名称
        var startUserName = req.body.startUserName //发起人名称
        var date = req.body.date //发起时间


        for (var i = 0; i < userlist.length; i++) {
            msg.sendemail(userlist[i], {
                title: '您好 您有新代办' + template_name,
                username: '系统通知',
                time: +new Date(),
                status: 0,
                message: '代办' + template_name + '\r\n发起人：' + startUserName + '\r\n发起时间:' + pu.formatDateTime(1, date)
            })
        }

        msg.sendmsg(userlist, {
            title: '您好 您有新代办' + template_name,
            username: '系统通知',
            time: +new Date(),
            status: 0,
            message: '代办' + template_name + '\r\n发起人：' + startUserName + '\r\n发起时间:' + pu.formatDateTime(1, date)
        })
        pu.success('OK', '', res)
    }

    async update_notice (req, res, next) {
        let userid = req.headers.uid
        let id = req.body.id
        let notice = req.body.notice
        let status = req.body.status

        if (!id) {
            pu.fail('参数不正确', res)
            return
        }
        if (status == 1) {
            for (var i = 0; i < id.length; i++) {
                await redis.hdel('USER_NOTICE_' + userid, id[i])
            }
            pu.success('OK', '', res)
            return
        }
        if (notice) {
            for (var i = 0; i < id.length; i++) {
                await redis.hset('USER_NOTICE_' + userid, id[i], JSON.stringify(notice[i]))
            }
            pu.success('OK', notice, res)
        } else {
            pu.success('OK', [], res)
        }
    }

    get_code (req, res, next) {
        var uuid = req.query.uuid
        if (!uuid) {
            pu.fail('参数不正确uuid', res)
            return
        }

        const cap = svgCaptcha.createMathExpr({
            // 翻转颜色
            // background: '#658FD2',
            // 字体大小
            fontSize: 30,
            // 噪声线条数
            noise: 1,
            // 宽度
            width: 105,
            // 高度
            height: 33,
            color: false
        })
        if (uuid) {
            redis.set('IMG_CODE_' + uuid, cap.text.toUpperCase(), 120)
        }
        res.type('svg') // 响应的类型
        res.send(cap.data)
    }

    async get_sms_code (req, res, next) {
        let phone = req.body.phone
        let imgcode = req.body.code
        let uuid = req.body.uuid
        let templateId = req.body.templateId


        let isnull = pu.isnull([phone, imgcode, uuid, templateId])
        if (isnull) {
            pu.fail('参数不正确', res)
            return
        }
        let code = await redis.get('IMG_CODE_' + uuid)
        if (!code) {
            pu.fail('验证码过期', res)
            return
        }
        if (code != imgcode.toUpperCase()) {
            pu.fail('验证码不正确', res)
            return
        }

        if (!pu.checkPhone(phone)) {
            pu.fail('手机号格式不正确', res)
            return
        }
        let smscode = Math.random().toString().slice(2, 8)
        redis.set('SMS_CODE_' + phone, smscode, 500)
        pu.success('发送成功', smscode, res)
        console.log('发送成功', phone, smscode)
        return
        let sendcode = await sms.sendCode([phone], templateId, [smscode])
        if (sendcode === true) {
            pu.success('发送成功', '', res)
        } else {
            pu.fail('发送失败' + sendcode, res)
        }
    }

    async get_system_template (req, res, next) {

    }


    // async palysuccesc (req, res, next) {
    //
    //
    // }
    // 获取是否压缩配置
    async get_compress (req, res, next) {
        pu.success('成功', config.compressConfig.compress, res)
    }
}

module.exports = new indexapi()
